home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / DOC / ASM.TXT next >
Text File  |  1996-08-02  |  3KB  |  77 lines

  1. Embedding assembly code into C/C++ programs with Watcom C/C++
  2.  
  3. Why the _asm keyword is not supported.
  4.  
  5. Microsoft and Borland have an _asm keyword that is provided with their 
  6. code, that allows in-line assembly code to coexist within a C or C++ 
  7. program.  Instead, Watcom uses a #pragma statement to declare a section 
  8. of assembly code as an auxiliary function.  The developer can specify 
  9. parameters to be passed to this code, which registers they go into, and 
  10. what registers will be modified. Watcom prefers to use this 
  11. implementation, and does not support the _asm implementation, for the 
  12. following reasons:
  13.  
  14. 1. The _asm directive does not tell the code generator what registers 
  15. have been modified.  Therefore, the optimizer cannot exploit maximum 
  16. speed advantages.  Assembly code is usually written for speed-critical 
  17. sections of a program, and having to save and restore all registers when 
  18. calling the routine
  19. would add program overhead where it is least desirable. Microsoft and 
  20. Borland turn off optimizations around assembly code, but Watcom finds 
  21. out which registers are modified to take advantage of maximum 
  22. optimization.
  23.  
  24.  2. The pragma statement is much more flexible than the _asm statement.  
  25. It allows you to tell the code generator which registers are modified by 
  26. the assembly language.  It also lets you define 'parameters' to your 
  27. routine.  For example, using a pragma, you could do something like the 
  28. following:
  29.  
  30. #pragma aux foo= "int 21" parm [ah] [ es bx ];
  31.  
  32.      then
  33.  
  34. foo( 1, &p );
  35.  
  36.      will automatically put the parameters 1 into ah and &p into es:bx.
  37.  
  38. You could also call "foo( 2, &q )"
  39.  
  40. Using the _asm statement, you cannot create generalized inline assembly 
  41. like this.  The examples would have to be coded separately, as follows:
  42.  
  43.      ASM( mov ah,1 )
  44.      ASM( mov es,seg p );
  45.      ASM( mov bx,offset p );
  46.      ASM( int 21 );
  47.  
  48. This would only work if 'p' were a static/extern variable, not an auto.
  49.  
  50. 3. #pragma's are quite easy to create.  The following creates a DOS 
  51. assembly function to set the screen to mode 4:
  52.  
  53. void mode4( void );
  54. #pragma aux mode4 =    \
  55.     "mov AH,0",    \
  56.     "mov AL,4",    \
  57.     "int 10H"        \
  58.     modify [ AH,  AL ];
  59.  
  60. This function can be called by using "mode4();" anywhere in a program.
  61.  
  62. 4. The code is kept more portable.  All of the machine dependent code is 
  63. kept in callable auxiliary functions, with an explicit interface.  This 
  64. makes the code very easy to port to other processors, which is 
  65. especially important with the movement toward RISC-based PCs.
  66.  
  67. 5. Pragmas should generally be needed only for specialty processor-
  68. specific functions, such as "lidt" or "out".  In this case, it is easier 
  69. to code the instruction once, and call it using a function.
  70.  
  71. At this time, the #pragma auxiliary functions will only take 128 bytes 
  72. of code at a time.  For existing programs that use large amounts of 
  73. assembly code, it is recommended that this code be put into a separate 
  74. assembly language file, and be assembled as a function.  The optimizer 
  75. will then expand this code inline, and the developer should notice no 
  76. difference in performance.
  77.